/**
 * About Dialog
 * 
 * @author remo.lemma@lu.unisi.ch & patrick.zulian@lu.unisi.ch
 */

package ch.unisi.inf.projects.java.uom.gui;

import javax.swing.JDialog;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import java.awt.Dimension;
import javax.swing.Box;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URI;
import ch.unisi.inf.projects.java.uom.gui.UOMImages;
import ch.unisi.inf.projects.java.uom.gui.commands.IconedCommand;

public class AboutDialog extends JDialog {

    private static final long serialVersionUID = -1L;

	public AboutDialog()
	{
		super();
		this.setTitle("UOM - About");
        setResizable(false);
        this.setPreferredSize(new Dimension(280,350));
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosed (final WindowEvent ev) { UOMMainFrame.instance().setEnabled(true); }
		});
		UOMMainFrame.instance().setEnabled(false);
		makeDialog();
		this.setLocationRelativeTo(null);
        this.setVisible(true);
	}
	
	private void makeDialog()
	{
		final String lemmaMail = "mailto:remo.lemma@lu.unisi.ch";
		final String zulianMail = "mailto:patrick.zulian@lu.unisi.ch";
		final String lemmaPage = "http://atelier.inf.unisi.ch/~lemmar/";
		final String zulianPage = "http://atelier.inf.unisi.ch/~zulianp/";
		
		final String about = 
			"<html>" + 
			"<html><b><font size=\"14\">UOM</font></b><br></br>" +
			"<b>Universal Object Manager</b><br></br>" +
			"<br></br>The best and fastest way<br>to create and customize your own Item manager.<br></br>" +
			"<br></br><b>Version:</b> 0.1<br><b>Authors:</b></html>";
		final String lemmaMailString =
			"<html><a href=\"" + lemmaMail + "\">Remo Lemma</a></html>";
		final String zulianMailString = 
			"<html><a href=\"" + zulianMail + "\">Patrick Zualian</a></html>";
		final String visit = 
			"<html><br><b>Visit us on:</b><br></html>";
		final String lemmaPageString = 
			"<html><a href=\"" + lemmaPage + "\">Remo Lemma's HomePage</a><br></br></html>";
		final String zulianPageString = 
			"<html><a href=\"" + zulianPage + "\">Patrick Zulian's HomePage</a><br></br></html>";
		
		final JPanel panel = new JPanel(new BorderLayout());
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		final JLabel label = new JLabel(about);
		final JLabel lemmaMailLabel = new JLabel(lemmaMailString);
		final JLabel zulianMailLabel = new JLabel(zulianMailString);
		final JLabel lemmaPageLabel = new JLabel(lemmaPageString);
		final JLabel zulianPageLabel = new JLabel(zulianPageString);
		
		final MouseAdapter adapter = new MouseAdapter () {
			private String parseHtml (final String link) {
				final int start = link.indexOf("\"", 0);
				final int end = link.indexOf("\"", start + 1);
				return link.substring(start + 1, end);
			}
			
			public void mousePressed (final MouseEvent ev) {
				final String text = parseHtml(((JLabel) ev.getSource()).getText());
				try {
					final URI uri = new URI(text);
					if (text.startsWith("mailto:"))
						Desktop.getDesktop().mail(uri);
					else
						Desktop.getDesktop().browse(uri);
				}
				catch (final URISyntaxException urie) {
					urie.printStackTrace();
					return;
				}
				catch (final IOException ioe) {
					ioe.printStackTrace();
					return;
				}
			}
		};
		
		lemmaMailLabel.addMouseListener(adapter);
		zulianMailLabel.addMouseListener(adapter);
		lemmaPageLabel.addMouseListener(adapter);
		zulianPageLabel.addMouseListener(adapter);

		final JPanel textPanel = new JPanel();
		textPanel.setLayout(new BoxLayout(textPanel,BoxLayout.Y_AXIS));
		textPanel.add(label);
		textPanel.add(lemmaMailLabel);
		textPanel.add(zulianMailLabel);
		textPanel.add(new JLabel(visit));
		textPanel.add(lemmaPageLabel);
		textPanel.add(zulianPageLabel);
		panel.add(textPanel);
		final JButton button = new JButton (new IconedCommand ("", "Visit us!", UOMImages.OK.getFileName()) {
		    private static final long serialVersionUID = -1L;
			public void actionPerformed (final ActionEvent ev) {
				dispose();
			}
		});
		final JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.add(Box.createHorizontalGlue());
		buttonPanel.add(button);
		buttonPanel.add(Box.createHorizontalStrut(5));
		panel.add(Box.createVerticalGlue());
		panel.add(buttonPanel);
		panel.add(Box.createVerticalStrut(5));
		this.getContentPane().add(panel, BorderLayout.EAST);
		this.pack();
	}
}
